package io.github.gaisad.sbvueblog.service;


import io.github.gaisad.sbvueblog.dao.BlogRepository;
import io.github.gaisad.sbvueblog.dto.BlogDTO;
import io.github.gaisad.sbvueblog.entity.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BlogService {

    @Autowired
    BlogRepository blogRepository;

    public void add(BlogDTO blogDTO){
        Blog blog = new Blog();
        if (blogDTO.getId() != null){
            blog = blogRepository.findById(blogDTO.getId()).get();
            blog.setAuthor("正常应该修改时间");
        } else {
            // 前端没有传递的数据，而后面又需要的可以在 service 层进行添加
            blog.setCreated_at(LocalDateTime.now());
            blog.setAuthor("当前登录用户");
        }

        blog.setTitle(blogDTO.getTitle());
        blog.setContent(blogDTO.getContent());



        blogRepository.save(blog);
    }
}